//
//  HBTaskController.m
//  HeartBeat
//
//  Created by karong on 2023/8/29.
//

#import "HBTaskController.h"
#import "HBSignModel.h"
#import "HBTaskModel.h"
#import "HBSignCell.h"
#import "HBTaskCell.h"
#import "AppDelegate.h"

@interface HBTaskController ()<WSSignCellDelegate, WSTaskCellDelegate>
@property (weak, nonatomic) IBOutlet UICollectionView *listView;

@property (nonatomic, strong) HBTask * taskData;
@property (nonatomic, strong) HBSign * signData;
@end

@implementation HBTaskController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self defaultRequest];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorFromHexRGB:@"FFF7EC"];
}

- (void)styleDidChange {
    self.view.backgroundColor = [UIColor colorFromHexRGB:@"FFF7EC"];
}

- (void)defaultRequest {
    [self loadSignListRequest];
    [self loadTaskListRequest];
}

- (void)loadSignListRequest {
    [HBRequest loadSignListWithSuccess:^(id obj) {
        self.signData = obj;
        [self.listView reloadData];
    } failed:^(NSURLResponse * _Nonnull response, NSError * _Nonnull error, id  _Nonnull responseObject) {
        
    }];
}

- (void)loadTaskListRequest {
    [HBRequest loadTaskListWithSuccess:^(id obj) {
        self.taskData = obj;
        [self.listView reloadData];
    } failed:^(NSURLResponse * _Nonnull response, NSError * _Nonnull error, id  _Nonnull responseObject) {
        
    }];
}

- (void)signRequest {
    [HBRequest signWithSuccess:^{
        HBSignModel * sign = self.signData.items[self.signData.sign_days];
        self.signData.is_sign_today = 1;
        self.signData.sign_days ++;
        [self.listView reloadItemsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]]];
        HBTaskModel * task = [HBTaskModel new];
        task.coins = sign.coins;
        [self showReceiveSuccessAlter:task];
    } failed:^(NSURLResponse * _Nonnull response, NSError * _Nonnull error, id  _Nonnull responseObject) {
    }];
}

- (void)receiveRequest:(HBTaskModel *)task {
    [HBRequest receiveWithTask_id:task.id success:^{
        [self defaultRequest];
        [self showReceiveSuccessAlter:task];
    } failed:^(NSURLResponse * _Nonnull response, NSError * _Nonnull error, id  _Nonnull responseObject) {
    }];
}

- (void)showReceiveSuccessAlter:(HBTaskModel *)task {
    [ViewManager showTaskSuccessVCWithModel:task inController:self];
}
#pragma mark UICollectionViewDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 3;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    NSInteger count = 0;
    if (section == 0 && self.signData.items.count > 0) {
        count = 1;
    }
    else if (section == 1 && self.taskData.newbie_task.count > 0) {
        count = 1;
    }
    else if (section == 2 && self.taskData.day_task.count > 0) {
        count = 1;
    }
    return count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HBTaskCell * cell;
    if (indexPath.section == 0) {
        cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HBSignCell" forIndexPath:indexPath];
        [cell setDataWithObject:self.signData];
        
    }
    else if (indexPath.section == 1) {
        cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HBTaskCell" forIndexPath:indexPath];
        [cell setDataWithObject:self.taskData.newbie_task type:1];
    }
    else {
        cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HBTaskCell" forIndexPath:indexPath];
        [cell setDataWithObject:self.taskData.day_task type:0];
    }
    cell.delegate = self;
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGSize size = CGSizeZero;
    size.width = HBScreenWidth - 12 - 12;
    if (indexPath.section == 0 && self.signData.items.count > 0) {
        size.height = 274;
    }
    else if (indexPath.section == 1 && self.taskData.newbie_task.count > 0) {
        size.height = 27 + self.taskData.newbie_task.count * 77;
    }
    else {
        size.height = 27 + self.taskData.day_task.count * 77;
    }
    
    return size;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    UIEdgeInsets inset = UIEdgeInsetsZero;
    inset.bottom = 12;
    return inset;
}

#pragma mark HBTaskCellDelegate
- (void)taskCellTurnActionWithType:(NSInteger)type {
    [ViewManager viewControllJumpWithjumpType:type vc:self dataDic:@{}];
}

-(void)taskCellReceiveActionWithObject:(HBTaskModel *)object {
    //領取
    [self receiveRequest:object];
}

#pragma mark WSSignCellDelegate
- (void)signCellSignAction {
    [self signRequest];
}

- (void)signCellRoleAction {
    [ViewManager showTaskRoleAlterAlterInController:self];
}

@end
